﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IcqSharp;
using IcqSharp.Connections;

namespace ISIM
{
    public class IcqClass
    {
        public Session session;

        public IcqClass(string uin, string password)
        {
            IcqSharp.Util.Log.Enable = true;

            session = new Session(uin, password);

            session.Connected += new EventHandler(session_Connected);
            session.LoginFailed += session_LoginFailed;

            session.Messaging.TypingNotification += new IcqSharp.Base.TypingNotificationDelegate(Messaging_TypingNotification);
            session.Messaging.MessageReceived += new IcqSharp.Base.MessageEventDelegate(messaging_MessageReceived);
            session.ContactList.ContactAdded += new IcqSharp.Base.ContactEventDelegate(ContactList_ContactAdded);
            session.ContactList.ContactStatusChanged += new IcqSharp.Base.ContactEventDelegate(ContactList_ContactStatusChanged);
        }

        void session_LoginFailed(object sender, EventArgs e)
        {
            Global.ISIMFunctions.ShowMessage("icq_login_failed", Global.LangManager.Translate("connectionFailed"), "ICQ: " + Global.LangManager.Translate("connectionFailed") + ". " + Global.LangManager.Translate("checkLoginAndInternetConnection"), System.Windows.Forms.MessageBoxIcon.Error);
        }


        void ContactList_ContactStatusChanged(IcqSharp.Base.Contact contact)
        {
            Global.ISIMFunctions.EditContactAvailability(contact.Uin, Global.Converter.GetContactAvailability(contact.Status));
        }

        void Messaging_TypingNotification(IcqSharp.Base.Contact c, IcqSharp.Base.TypingNotification n)
        {
            // kontakt píše zprávu
            Global.ISIMFunctions.ContactIsComposing(c.Uin);
        }

        void ContactList_ContactAdded(IcqSharp.Base.Contact contact)
        {
            Console.WriteLine("icq adding: " + contact.Uin + ":" + contact.Nickname);

            Global.OpenForms.mainForm.contactListView.BeginInvoke(new System.Windows.Forms.MethodInvoker(() =>
            {
                if (!Global.OpenForms.mainForm.contactListView.Items.ContainsKey(contact.Uin))
                {
                    Global.ISIMFunctions.AddContact(new IcqContact(contact));
                }
            }));
        }

        void messaging_MessageReceived(IcqSharp.Base.Message message)
        {
            Console.WriteLine(message.Text);
            if ((message.Text != null) && (message.Text.Replace(" ", "").Length > 0))
            {
                Global.OpenForms.mainForm.Invoke(new System.Windows.Forms.MethodInvoker(() =>
                {
                    ISIMContact contact;
                    if (Global.OpenForms.mainForm.contactListView.Items.ContainsKey(message.Contact.Uin))
                        contact = (Global.OpenForms.mainForm.contactListView.Items[message.Contact.Uin] as ContactListViewItem).contact;
                    else
                        contact = new IcqContact(message.Contact);

                    Global.ISIMFunctions.AddChatMessage(contact, message.Text, message.Timestamp, contact.name);
                }));
            }
        }

        void session_Connected(object sender, EventArgs e)
        {
            Console.WriteLine("connected to ICQ");
            Global.ISIMFunctions.CloseMessage("icq_login_failed");
        }

        public void AddContact(string uin)
        {
            session.ContactList.Add(new IcqSharp.Base.Contact(uin));
        }

        public void connect()
        {
            session.Connect();
            session.Status = IcqSharp.Base.Status.Online;
        }

        public void disconnect()
        {
            try
            {
                session.Status = IcqSharp.Base.Status.Offline;
            }
            catch
            {
                Console.WriteLine("icq cannot set status to offline");
            }
            session.Disconnect();
        }
    }
}
